package com.bike.controller.information;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.bike.cache.UserCache;
import com.bike.controller.BaseController;
import com.bike.entity.HhAgent;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhWithdrawAgent;
import com.bike.entity.SysUser;
import com.bike.service.impl.HhAgentServiceImpl;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.service.impl.HhWithdrawAgentServiceImpl;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.StrUtil;
import com.topit.show.aliapi.SmsUtil;

 
@Controller
@RequestMapping(value="/hhWithdrawAgent")
public class HhWithdrawAgentController extends BaseController {
	
		@Autowired
		private HhWithdrawAgentServiceImpl  hhWithdrawAgentService;
		@Autowired
		private HhAgentServiceImpl  hhAgentServiceImpl;
		@Autowired
		private HhBusinessUserServiceImpl  businessUserServiceImpl;
		@Autowired
		private UserCache  usercache;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/information/withdrawAgent/hhWithdrawAgent_list";
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhWithdrawAgentService.selectHhWithdrawAgentList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhWithdrawAgentService.selectHhWithdrawAgentList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			if(pd.getLong("withdrawAgentId")>0){
				model.addAttribute("pd", hhWithdrawAgentService.selectByHhWithdrawAgentId(pd.getLong("withdrawAgentId")));
			} 
			return "/information/withdrawAgent/hhWithdrawAgent_edit";
		}
		/**
		 * 搜素
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("toSearch")
		public String toSearch(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd", pd);
			return "/information/withdrawAgent/hhWithdrawAgent_search";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,HhWithdrawAgent hhWithdrawAgent){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				hhWithdrawAgentService.saveOrUpdate(pd,hhWithdrawAgent);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("withdrawAgentId")>0){
					hhWithdrawAgentService.deleteHhWithdrawAgentById(pd.getLong("withdrawAgentId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
	 	

	 	/**
	 	 * to代理商提现
	 	 */
	 	@RequestMapping("toAgentWithdraw")
	 	public String toAgentWithdraw(HttpServletRequest request,Model model){
	 		PageData pd=PageData.getPageDataByPage(request);
	 		
	 		SysUser user=(SysUser) request.getSession().getAttribute("user");
	 		HhAgent agent=hhAgentServiceImpl.selectByHhAgentId(user.getAgentId());
	 		
	 		//hhWithdrawAgentService.updateAgentWithdraw(agent);
	 		model.addAttribute("agent", agent);
	 		BigDecimal wxFee=agent.getBalance().multiply(new BigDecimal(Constant.WXwithdrawFee)).setScale(2, RoundingMode.HALF_UP);
	 		if(wxFee.doubleValue()==0){
	 			wxFee=new BigDecimal("0.01");
	 		}
	 		model.addAttribute("wxFee", wxFee);
	 		model.addAttribute("trueAccount", agent.getBalance().subtract(new BigDecimal("2")).subtract(wxFee));
	 		return "/information/home/agent_withdraw";
	 	}
	 	
	 	/**
		 * 发送手机验证码
		 */
		@RequestMapping("sendCode")
		@ResponseBody
		public PageData sendCode(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			 //发短信
	        SendSmsResponse response;
			try {
				String code=String.valueOf((int)((Math.random()*9+1)*100000));
				//String code="1234";
				SysUser user1=(SysUser) request.getSession().getAttribute("user");
				HhAgent agent=hhAgentServiceImpl.selectByHhAgentId(user1.getAgentId());
				String phone= agent.getMobile();
				
				if(!StrUtil.isChinaPhoneLegal(phone)){
					return getErrorPd("手机号码格式不对,请修改个人信息");
				}
				
				HhBusinessUser user=businessUserServiceImpl.selectByPhone(phone);
				
				if(user==null){
					return getErrorPd("用户不存在");
				}
				response = SmsUtil.sendLoginCode(phone,code);
				if(response.getCode().equals("OK")){
					usercache.putLoginCodeCacheValue(phone, code);
			 }
			} catch (Exception e) {
				e.printStackTrace();
			}
			PageData successPd=getSuccessPd();
			return  successPd;
		}
		/**
		 * 代理商提现
		 */
		@RequestMapping("agentWithdraw")
		@ResponseBody
		public PageData agentWithdraw(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			String code=pd.getString("code");
			
			SysUser user=(SysUser) request.getSession().getAttribute("user");
			HhAgent agent=hhAgentServiceImpl.selectByHhAgentId(user.getAgentId());
			
			String cacheCode=usercache.getLoginCodeCacheValue(agent.getMobile());
			if(StringUtils.isEmpty(cacheCode)||!code.equals(cacheCode)){
				return getErrorPd("验证码错误");
			}
			
			  hhWithdrawAgentService.updateAgentWithdraw(agent);
			  return getSuccessPd();
		}
		/**
		 * 代理商提现完成
		 * param  withdrawId
		 */
		@RequestMapping("agentWithdrawOk")
		@ResponseBody
		public PageData agentWithdrawOk(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			 
			long withdrawId=pd.getLong("withdrawAgentId");
			HhWithdrawAgent hhWithdraw=hhWithdrawAgentService.selectByHhWithdrawAgentId(withdrawId);
			if(hhWithdraw.getStatus()==1){
				return getErrorPd("已经完成");
			}
			hhWithdrawAgentService.updateAgentWithdrawOK(withdrawId);
			return getSuccessPd();
		}
}
